package com.rms.hkpc.presenter;


import com.google.gson.reflect.TypeToken;
import com.rms.hkpc.apiresult.ApiResultData;
import com.rms.hkpc.constant.AllConstants;
import com.rms.hkpc.db.dto.ShortDto;
import com.rms.hkpc.db.dto.VersionDto;
import com.rms.hkpc.model.ComboModel;
import com.rms.hkpc.model.SettingsModel;
import com.rms.hkpc.model.VersionModel;
import com.rms.hkpc.model.listener.OnResultListener;
import com.rms.hkpc.model.listener.OnResultProgressListener;
import com.rms.hkpc.util.SPUtil;
import com.rms.hkpc.view.ISettingsView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;
import java.util.Locale;

public class SettingsPresenter extends BasePresenter<ISettingsView> {
    private SettingsModel settingsModel = new SettingsModel();
    private VersionModel model = new VersionModel();
    private ComboModel comboModel = new ComboModel();

    public void getProject() {
        getView().showLoading();
        comboModel.combo("projs",new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onGetProjectSuccess((ApiResultData<List<ShortDto>>) apiResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onGetProjectFail(e);
                }
            }
        }, new TypeToken<ApiResultData<List<ShortDto>>>() {
        }.getType());
    }

    public void changeProject(final String projectName, final String projectId) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userId", SPUtil.getInstance().getString(AllConstants.ID));
            jsonObject.put("projId", projectId);
            settingsModel.changeProject(jsonObject.toString(), new OnResultListener() {
                @Override
                public void onSuccess(Object apiResult) {
                    if (getView() != null) {
                        getView().onUploadProjectSuccess((ApiResultData<String>) apiResult, projectName, projectId);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().onUploadProjectFail(e);
                    }
                }
            }, new TypeToken<ApiResultData<String>>() {
            }.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void changeLanguage(final int languageId, final Locale locale) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userId", SPUtil.getInstance().getString(AllConstants.ID));
            jsonObject.put("langId", languageId);
            settingsModel.changeLanguage(jsonObject.toString(), new OnResultListener() {
                @Override
                public void onSuccess(Object apiResult) {
                    if (getView() != null) {
                        getView().onUploadLanguageSuccess((ApiResultData<String>) apiResult, languageId, locale);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().onUploadLanguageFail(e);
                    }
                }
            }, new TypeToken<ApiResultData<String>>() {
            }.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void getVersion() {
        getView().showLoading();
        model.getVersion(new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onGetVersionSuccess((ApiResultData<VersionDto>) aipResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onGetVersionFails(e);
                }
            }
        }, new TypeToken<ApiResultData<VersionDto>>() {
        }.getType());
    }

    public void downloadFile(String fileName, File file) {
        model.download(fileName, file, new OnResultProgressListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().onDownloadFileSuccess((File) aipResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onDownloadFails(e);
                }
            }

            @Override
            public void onProgress(int progress) {
                if (getView() != null) {
                    getView().onDownloadProgress(progress);
                }
            }
        });
    }

    public void logOut() {
        getView().showLoading();
        settingsModel.logOut(SPUtil.getInstance().getString(AllConstants.ID), new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onLogoutSuccess((ApiResultData<String>) apiResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onLogoutFail(e);
                }
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }
}
